package com.kc.sjwh.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.kc.sjwh.entity.bo.UserBo;
import com.kc.sjwh.entity.po.Czlly;
import com.kc.sjwh.other.AjaxRes;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.service.CzllyService;

/**
 * 处置力量页action
 * @author xianding
 * @date 2017年11月28日
 */
@Controller
@RequestMapping("czlly")
public class CzllyAction extends BaseAction {
	static Logger logger = Logger.getLogger(CzllyAction.class);
	
	@Autowired
	CzllyService service;
	
	/**
	 * 打开列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest req) {
		return "czlly/czllyList";
	}
	/**
	 * 打开列表页
	 * @param req
	 * @return
	 */
	@RequestMapping("list1")
	public String list1(HttpServletRequest req) {
		return "czlly/czllyList1";
	}
	
	/**
	 * 打开树
	 * @param req
	 * @return
	 */
	@RequestMapping("tree")
	public String tree(HttpServletRequest req) {
		req.setAttribute("treeJson", JSON.toJSONString(service.getList()));	
		return "sys/tree4czll";
	}
	
	/**
	 * 分页查询
	 * @param req
	 * @param pageSize
	 * @param pageNum
	 * @return
	 */
	@ResponseBody
	@RequestMapping("getPage")
	public String getPage(HttpServletRequest req, int pageSize, int pageNum) {
		Map<String, String> m = super.getParamMap(req);

		if(!SecurityUtils.getSubject().hasRole("admin")) {
			m.put("dwdm", getLoginUser().getSsdwdm());
		}
		
		Page<Czlly> page = service.getPage(pageSize, pageNum, m);
//		
//		StringBuffer sb = new StringBuffer();
//		for(Czlly one : page.getList()) {//数据行
//			CzllyBo bo = (CzllyBo)one;
//			sb.append("<tr>");
//			sb.append("<td>"+new SimpleDateFormat("yy/MM/dd HH:mm").format(bo.getCjsj())+"</td>");
//			sb.append("<td>"+chk(bo.getYbh())+"</td>");
//			sb.append("<td>"+chk(bo.getYmc())+"</td>");
//			sb.append("<td>"+chk(bo.getSsdwmc())+"</td>");
//			sb.append("<td>");
////			sb.append("	<a  data-toggle='modal' data-iframe='form?id="+bo.getYbh()+"' data-size='lg'>核查</a> &nbsp;");
//			sb.append("	<a href='form?id="+bo.getYbh()+"'>编辑</a> &nbsp;");
//			sb.append("	<a href='../czllz/form?ybh="+bo.getYbh()+"'>新增组</a> &nbsp;");
//			sb.append("	<a onclick=\"deletes('"+bo.getYbh()+"')\">删除</a>");
//			sb.append("</td>");
//			sb.append("</tr>");
//		}
//		
//		//返回结果
//		Map data = new HashMap();
//		data.put("total", page.getTotal());
//		data.put("html", sb.toString());
		return JSON.toJSONString(new AjaxRes(page));
	}
	
	/**
	 * 打开表单(新增或修改)
	 * @param id
	 */
	@RequestMapping("form")
	public String form(HttpServletRequest req, String id) {
		req.setAttribute("act", id==null?"add":"update");
		req.setAttribute("actName", id==null?"新增":"修改");
		req.setAttribute("one", service.form(id));
		req.setAttribute("loginUser", super.getLoginUser());
		return "czlly/czllyForm";
	}
	
	/**
	 * 保存（新增或修改）
	 * @param one
	 * @param act add/update
	 */
	@ResponseBody
	@RequestMapping("save")
	public String save(Czlly one, String act) {
		if("add".equals(act)) {
			UserBo user = getLoginUser();
			one.setCjr(user.getMc());
			if(StringUtils.isEmpty(one.getSsdwdm())) {
				one.setSsdwdm(user.getSsdwdm());
				one.setSsdwmc(user.getSsdwmc());
			}
			service.add(one);
		} else {
			one.setGxr(getLoginUser().getMc());
			service.update(one);
		}
		return JSON.toJSONString(new AjaxRes());
	}
	
	/**
	 * 删除
	 * @param req
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delete")
	public String delete(HttpServletRequest req, String id) {
		service.delete(id);
		return JSON.toJSONString(new AjaxRes());
	}
	
//	/**
//	 * 下载模板
//	 * @param req
//	 * @param res
//	 */
//	@RequestMapping("downLoadFile")
//	public void downLoadFile(HttpServletRequest req,HttpServletResponse res){
//		try {
//			String flieName="人员信息.xls";
//			String url = req.getRealPath("/") + "static/doc/"+flieName;
//			File file = new File(url);
//			InputStream fis = new BufferedInputStream(new FileInputStream(url));
//		    byte[] buffer = new byte[fis.available()];
//		    fis.read(buffer);
//		    fis.close();
//		    res.reset();
//		    res.addHeader("Content-Disposition", "attachment;filename=" + new String(flieName.getBytes("utf-8"),"iso8859-1"));
//		    res.addHeader("Content-Length", "" + file.length());
//		    OutputStream os = new BufferedOutputStream(res.getOutputStream());
//		    res.setContentType("application/octet-stream");
//		    os.write(buffer);// 输出文件
//		    os.flush();
//		    os.close();
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	/**
//	 * 批量导入
//	 * @return 
//	 */
//	@ResponseBody
//	@RequestMapping("batchImport")
//	public String  batchImport(@RequestParam("file") CommonsMultipartFile file,
//			HttpServletRequest req,HttpServletResponse res) throws IOException, ParseException {
//       System.out.println("fileName："+file.getOriginalFilename());
//       String path= req.getRealPath("/") + "tmp/"+Tool.getUuid()+".xls";    
//       File newFile=new File(path);
//       //通过CommonsMultipartFile的方法直接写文件（注意这个时候）
//       file.transferTo(newFile);
//       HSSFWorkbook hwb = new HSSFWorkbook(new FileInputStream(newFile));
//       HSSFSheet sheet = hwb.getSheetAt(0);
//       Object value = null;
//       HSSFRow row = null;
//       HSSFCell cell = null;
//       UserBo user = getCurrentUser();
//       for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
//    	   if (i != sheet.getFirstRowNum()) {			
//    		   row = sheet.getRow(i);
//    		   Czlly one=new Czlly();
//    		   //根据行设置人员
//    		   service.setCzllyByRow(row,one);
//    		   one.setCjr(user.getMc());
//    		   service.add(one);
//    		   System.out.println(one);
//    	   }
//       }
//       newFile.delete();
//       return JSON.toJSONString(new AjaxRes());
//   }
}
